package org.elasticsearch.aggregate;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.AggregatorFactory;
import org.elasticsearch.search.aggregations.support.*;
import org.elasticsearch.search.internal.SearchContext;

import java.io.IOException;
import java.util.Map;

public class TimeLimitingAggregationBuilder extends ValuesSourceAggregationBuilder<ValuesSource.Numeric, TimeLimitingAggregationBuilder> {

    private final static Logger logger = LogManager.getLogger(TimeLimitingAggregationBuilder.class);
    public static final String NAME = "limiting";

    private static final ObjectParser<TimeLimitingAggregationBuilder, Void> PARSER;

    static {
        PARSER = new ObjectParser<>(TimeLimitingAggregationBuilder.NAME);
        ValuesSourceParserHelper.declareNumericFields(PARSER, false,false, false);
    }

    public static AggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException {
        return PARSER.parse(parser, new TimeLimitingAggregationBuilder(aggregationName), null);
    }

    private TimeLimitingAggregationBuilder(String name) {
        super(name, ValuesSourceType.BYTES, ValueType.NUMBER);
    }

    public TimeLimitingAggregationBuilder(StreamInput in) throws IOException {
        super(in, ValuesSourceType.BYTES, ValueType.NUMBER);
    }

    private TimeLimitingAggregationBuilder(ValuesSourceAggregationBuilder<ValuesSource.Numeric, TimeLimitingAggregationBuilder> clone, AggregatorFactories.Builder factoriesBuilder, Map<String, Object> metaData) {
        super(clone, factoriesBuilder, metaData);
    }

    @Override
    protected void innerWriteTo(StreamOutput streamOutput) throws IOException {

    }

    @Override
    protected ValuesSourceAggregatorFactory<ValuesSource.Numeric, ?> innerBuild(
            SearchContext searchContext, ValuesSourceConfig<ValuesSource.Numeric> valuesSourceConfig,
            AggregatorFactory<?> aggregatorFactory, AggregatorFactories.Builder builder) throws IOException {
        logger.warn("build");
        return new TimeLimitingAggregator.Factory(name, valuesSourceConfig, searchContext, aggregatorFactory, builder, metaData);
    }

    @Override
    protected XContentBuilder doXContentBody(XContentBuilder xContentBuilder, Params params) throws IOException {
        return xContentBuilder;
    }

    @Override
    protected int innerHashCode() {
        return 0;
    }

    @Override
    protected boolean innerEquals(Object o) {
        return false;
    }

    @Override
    protected AggregationBuilder shallowCopy(AggregatorFactories.Builder builder, Map<String, Object> map) {
        return new TimeLimitingAggregationBuilder(this, builder, metaData);
    }

    @Override
    public String getType() {
        return NAME;
    }
}
